<!-- 防疫提醒--弹窗 -->
<template>
    <el-dialog :title="info.title" :visible.sync="dialogVisible">
        <el-form ref="immunityForm" :model="immunityForm" :rules="immunityRules" label-width="110px">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="到期时间:" prop="time">
                        <el-date-picker v-model="immunityForm.time" type="date" placeholder="到期时间" style="width:100%"
                            value-format="yyyy-MM-dd HH:mm:ss">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="免疫项目:" prop="project">
                        <el-input v-model="immunityForm.project" placeholder="免疫项目" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="免疫对象:" prop="object">
                        <el-select v-model="immunityForm.object" placeholder="免疫对象" style="width:100%">
                            <el-option v-for="item in epidemicPreventionTargets" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="猪舍:" prop="pigsty">
                        <el-select v-model="immunityForm.pigsty" placeholder="猪舍" style="width:100%" clearable>
                            <el-option v-for="item in houses" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="需免疫数量:" prop="num">
                        <el-input v-model="immunityForm.num" style="width:245px" placeholder="需免疫数量" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm('immunityForm')">保存</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    props: ['info'],
    data() {
        return {
            dialogVisible: false,
            immunityForm: {
                id: '',
                time: '',
                project: '',
                object: '',
                pigsty: '',
                num: '',
            },
            immunityRules: {
                time: [
                    { required: true, message: this.$t('validate.required'), trigger: 'blur' }
                ],
                project: [
                    { required: true, message: this.$t('validate.required'), trigger: 'blur' }
                ],
                object: [
                    { required: true, message: this.$t('validate.required'), trigger: 'blur' }
                ],
                pigsty: [
                    { required: true, message: this.$t('validate.required'), trigger: 'blur' }
                ],
                num: [
                    { required: true, message: this.$t('validate.required'), trigger: 'blur' }
                ]
            },
            epidemicPreventionTargets: [
                {
                    value: '生长猪',
                    label: '生长猪'
                },
                {
                    value: '种猪',
                    label: '种猪'
                },
            ],
            houses: [],
        }
    },
    mounted() {
        this.getHouses();
    },
    methods: {
        // 新增接口----------------
        submitForm(formName) {
            //当全部验证字段通过，再开始业务逻辑
            this.$refs[formName].validate((valid) => {
                //如果全部字段符合条件
                if (valid) {
                    if (this.info.type == 1) {
                        delete this.immunityForm.id
                        this.$http.post(`/a`, this.immunityForm).then((res) => {
                            if (res.code == 0) {
                                this.$message({
                                    message: '恭喜你，新增成功',
                                    type: 'success'
                                });
                                this.dialogVisible = false
                                this.$emit('updateViews');
                            } else {
                                return this.$message.error(res.msg);
                            }
                        })
                    } else {
                        this.$http.put(`/a`, this.immunityForm).then((res) => {
                            if (res.code == 0) {
                                this.$message({
                                    message: '恭喜你，修改成功',
                                    type: 'success'
                                });
                                this.dialogVisible = false
                                this.$emit('updateViews');
                            } else {
                                return this.$message.error(res.msg);
                            }
                        })
                    }
                } else {
                    return false;
                }
            });
        },
        // 猪舍接口--------------
        getHouses() {
            this.$http.get(`/basic/house/all`).then(({ data: res }) => {
                if (res.code !== 0) {
                    return this.$message.error(res.msg)
                }
                this.houses = res.data
            }).catch(() => { })
        },
    }
}
</script>

<style scoped>
.el-col {
    margin-bottom: 3px;
}
</style>